﻿using Cloud.Core.Model;
using Cloud.Core.ModelSearch;
using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace Cloud.Core.Bizlogic
{
    public class Helper
    {
        public string ModuleName(string name)
        {
            string operateName = "";
            string[] arr = name.Split("-");
            switch (arr[0])
            {
                case "ADataInquiry": operateName = "数据查询"; break;
                case "AProdInquiry": operateName = "产量查询"; break;
                case "AZhengyb": operateName = "我的测试"; break;
                case "Balance": operateName = "设备管理"; break;
                case "Data": operateName = "数据管理"; break;
                case "Dept": operateName = "部门管理"; break;
                case "Frequency": operateName = "班次管理"; break;
                case "SystemInfo": operateName = "系统管理"; break;
                case "User":
                    if (arr[1] == "SignIn")
                    {
                        operateName = "登录退出";
                    }
                    else
                    {
                        operateName = "用户管理";
                    }
                    break;
                default: operateName = "其它"; break;
            }
            return operateName;
        }

  
        public static void TestAD()
        {
            while (true)
            {
                _ = new LoggerWriteen().writeAsync(new Log(Log.OperateType.异常, "TestAAAAAAAAAAAA"), "AAABBB");
                System.Threading.Thread.Sleep(5000);
            }
        }
        /// <summary>
        /// 内存对象转换为json字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string ObjectToJson(object obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            serializer.WriteObject(stream, obj);
            byte[] dataBytes = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(dataBytes, 0, (int)stream.Length);
            return Encoding.UTF8.GetString(dataBytes);
        }

        /// <summary>
        /// 内存对象转换为json字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string objToJson(object o)
        {
            if (o == null)
            {
                return null;
            }
            return JsonConvert.SerializeObject(o);
        }
        /// <summary>
        /// 搜索条件分页修改
        /// </summary>
        /// <param name="sPage"></param>
        public void sPageNew(sPage sPage)
        {
            if (sPage.PageIndex == null || sPage.PageIndex < 0 || sPage.PageIndex == 0)
            {
                sPage.PageIndex = 0;
            }
            else
            {
                sPage.PageIndex = sPage.PageIndex - 1;
            }
            if (sPage.PageSize == null || sPage.PageSize < 0)
            {
                sPage.PageSize = int.MaxValue;
            }
        }
        /// <summary>
        /// 搜索条件分页修改
        /// </summary>
        /// <param name="sPage"></param>
        public void sPageNew(sApple sPage)
        {
            if (sPage.PageIndex == null || sPage.PageIndex < 0 || sPage.PageIndex == 0)
            {
                sPage.PageIndex = 0;
            }
            else
            {
                sPage.PageIndex = sPage.PageIndex - 1;
            }
            if (sPage.PageSize == null || sPage.PageSize < 0)
            {
                sPage.PageSize = int.MaxValue;
            }
        }
    }
}
